package com.ruoyi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.DeviceMain;
import com.ruoyi.service.IDeviceMainService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备主表Controller
 * 
 * @author jack
 * @date 2025-01-05
 */
@RestController
@RequestMapping("/device/main")
public class DeviceMainController extends BaseController
{
    @Autowired
    private IDeviceMainService deviceMainService;

    /**
     * 查询设备主表列表
     */
//    @PreAuthorize("@ss.hasPermi('device:main:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeviceMain deviceMain)
    {
        startPage();
        deviceMain.setUserId(SecurityUtils.getUserId());
        List<DeviceMain> list = deviceMainService.selectDeviceMainList(deviceMain);
        return getDataTable(list);
    }

    /**
     * 导出设备主表列表
     */
//    @PreAuthorize("@ss.hasPermi('device:main:export')")
    @Log(title = "设备主表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeviceMain deviceMain)
    {
        List<DeviceMain> list = deviceMainService.selectDeviceMainList(deviceMain);
        ExcelUtil<DeviceMain> util = new ExcelUtil<DeviceMain>(DeviceMain.class);
        util.exportExcel(response, list, "设备主表数据");
    }

    /**
     * 获取设备主表详细信息
     */
//    @PreAuthorize("@ss.hasPermi('device:main:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(deviceMainService.selectDeviceMainById(id));
    }

    /**
     * 新增设备主表
     */
//    @PreAuthorize("@ss.hasPermi('device:main:add')")
    @Log(title = "设备主表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeviceMain deviceMain)
    {
        if(deviceMainService.selectDeviceMainBySn(deviceMain.getDeviceSn()) != null){
            return AjaxResult.error("设备编号已存在");
        }
        deviceMain.setUserId(SecurityUtils.getUserId());
        return toAjax(deviceMainService.insertDeviceMain(deviceMain));
    }

    /**
     * 修改设备主表
     */
//    @PreAuthorize("@ss.hasPermi('device:main:edit')")
    @Log(title = "设备主表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceMain deviceMain)
    {
        return toAjax(deviceMainService.updateDeviceMain(deviceMain));
    }

    /**
     * 删除设备主表
     */
//    @PreAuthorize("@ss.hasPermi('device:main:remove')")
    @Log(title = "设备主表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deviceMainService.deleteDeviceMainByIds(ids));
    }
}
